package com.applicationLayer.action;

import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import com.applicationLayer.formbean.CommentForm;
import com.applicationLayer.formbean.CovoitureurForm;
import com.applicationLayer.formbean.PathForm;
import com.applicationLayer.formbean.StepForm;
import com.applicationLayer.sessionbean.SessionBean;
import com.common.domainService.ICommentService;
import com.common.domainService.IPathService;
import com.common.domainService.IStepService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DetailsPathAction extends ActionSupport {
	
	private static final long serialVersionUID = 1367892627989934742L;

	Logger logger = Logger.getLogger(DetailsPathAction.class);
	
	private IPathService pathService;
	private IStepService stepService;
	private ICommentService commentDomainService;
	private CommentForm comment;
	private PathForm path;
	private List<CommentForm> commentList;
	private List<StepForm> steps;

	public String pathDetails(){
		final String id = (String) SessionBean.getFromSession("id_kept_path");
		if(id!=null){
			ActionContext.getContext().setParameters(new HashMap<String, Object>(){
				private static final long serialVersionUID = 4758725673330933824L;
				{put("id_path", id);}
			});
		}
		Object paramObj = ActionContext.getContext().getParameters().get("id_path");  
        if(paramObj != null){
        	String idP;
        	try{
        		idP=((String[])paramObj)[0];
        	}
        	catch(Exception e){
        		idP=String.valueOf(paramObj);
        	}
        	try{
	        	int id_path = Integer.parseInt(idP);
	        	commentList = commentDomainService.getPathComments(id_path);
	        	SessionBean.setInSession("commentList", commentList);
	        	path = pathService.getPathById(id_path);
	        	if(path==null)
	        		return "HOME";
	        	this.setSteps(this.getStepService().getStepsByPath(id_path));
	        	SessionBean.setInSession("pathDetails", path);
        	}
        	catch(NumberFormatException e){
        		return "HOME";
        	}
        }
        else{
        	logger.info("pathDetails() undone : path id param is missing");
        	return "HOME";
        }
        logger.info("pathDetails() done");
		return "SUCCESS";
		
	}
	
	public String postComment(){
		if(!SessionBean.checkUserInSession()){
			return "SUCCESS";
		}
		CovoitureurForm co = (CovoitureurForm) SessionBean.getFromSession("loggedUser");
		path = (PathForm) SessionBean.getFromSession("pathDetails");
		commentList = (List<CommentForm>) SessionBean.getFromSession("commentList");
		if(!validateField())
			return "COMMENT";
		commentDomainService.addPathComment(comment,co.getId_covoitureur(),path.getId_path());
		SessionBean.setInSession("id_kept_path",String.valueOf(path.getId_path()));
		return "SUCCESS";
	}
	
	public boolean validateField(){
		boolean isValid=true;
		if(comment!=null){
			if (comment.getComment().length()==0){	
				addFieldError( "comment.comment", " Content is required" );
				isValid=false;
			}
			if (comment.getTitle().length()==0){	
				addFieldError( "comment.title", " Title is required" );
				isValid=false;
			}
		}
		logger.info("validate() done");
		return isValid;
	}
	
	public String pathRegister(){
		if(!SessionBean.checkUserInSession()){
			return "SUCCESS";
		}
		CovoitureurForm co = (CovoitureurForm) SessionBean.getFromSession("loggedUser");
		path = (PathForm) SessionBean.getFromSession("pathDetails");
		if(path!=null){
			pathService.addCovoitureur(co.getId_covoitureur(),path);
			SessionBean.UnsetFromSession("pathDetails");
		}
		logger.info("pathRegister() done");
		return "SUCCESS";
	}
	
	public String pathUnregister(){
		if(!SessionBean.checkUserInSession()){
			return "SUCCESS";
		}
		CovoitureurForm co = (CovoitureurForm) SessionBean.getFromSession("loggedUser");
		path = (PathForm) SessionBean.getFromSession("pathDetails");
		if(path!=null){
			pathService.removeCovoitureur(co.getId_covoitureur(),path);
			SessionBean.UnsetFromSession("pathDetails");
		}
		logger.info("pathUnregister() done");
		return "SUCCESS";
	}
	
	public boolean isRegister(){
		if(!SessionBean.checkUserInSession()){
			return false;
		}
		CovoitureurForm co = (CovoitureurForm) SessionBean.getFromSession("loggedUser");
		PathForm pa = (PathForm) SessionBean.getFromSession("pathDetails");
		if(pa!=null){
			if(pathService.isCovoitureurRegisterForPath(co.getId_covoitureur(),pa.getId_path()))
				return true;
		}
		return false;
	}
	
	public boolean isDriver(){
		if(!SessionBean.checkUserInSession()){
			return false;
		}
		CovoitureurForm co = (CovoitureurForm) SessionBean.getFromSession("loggedUser");
		PathForm pa = (PathForm) SessionBean.getFromSession("pathDetails");
		if(pa!=null){
			return pa.getDriver()==co.getId_covoitureur();
		}
		return false;
	}

	/** GETTER **/

	public IPathService getPathService() {
		return pathService;
	}
	
	public PathForm getPath() {
		return path;
	}

	public List<CommentForm> getCommentList() {
		return commentList;
	}

	public ICommentService getCommentDomainService() {
		return commentDomainService;
	}
	
	public CommentForm getComment() {
		return comment;
	}

	public IStepService getStepService() {
		return stepService;
	}

	public List<StepForm> getSteps() {
		return steps;
	}

	/** SETTER **/
	
	public void setPathService(IPathService pathService) {
		this.pathService = pathService;
	}
	
	public void setPath(PathForm path) {
		this.path = path;
	}
	
	public void setCommentList(List<CommentForm> commentList) {
		this.commentList = commentList;
	}
	
	public void setCommentDomainService(ICommentService commentDomainService) {
		this.commentDomainService = commentDomainService;
	}
	
	public void setComment(CommentForm comment) {
		this.comment = comment;
	}

	public void setStepService(IStepService stepService) {
		this.stepService = stepService;
	}

	public void setSteps(List<StepForm> steps) {
		this.steps = steps;
	}
}